package com.ktdaddy.utils;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.databind.ObjectMapper;

/**
 *
 * @Title: ApiResponse.java
 * @Package com.ktdaddy.utils
 * @Description: 自定义响应数据结构
 * 				本类可提供给 H5/ios/安卓/公众号/小程序 使用
 * 				前端接受此类数据（json object)后，可自行根据业务去实现相关功能
 *
 * 				200：表示成功
 * 				500：表示错误，错误信息在msg字段中
 * 				501：bean验证错误，不管多少个错误都以map形式返回
 * 				502：拦截器拦截到用户token出错
 * 				555：异常抛出信息
 * 				556: 用户qq校验异常
 */
public class ApiResponse {

    // 定义jackson对象
    private static final ObjectMapper MAPPER = new ObjectMapper();

    // 响应业务状态
    private Integer code;

    // 响应消息
    private String message;

    // 响应中的数据
    private Object result;

    @JsonIgnore
    private String ok;	// 不使用

    public static ApiResponse build(Integer status, String msg, Object data) {
        return new ApiResponse(status, msg, data);
    }

    public static ApiResponse build(Integer status, String msg, Object data, String ok) {
        return new ApiResponse(status, msg, data, ok);
    }

    public static ApiResponse ok(Object data) {
        return new ApiResponse(data);
    }

    public static ApiResponse ok() {
        return new ApiResponse(null);
    }

    public static ApiResponse errorMsg(String msg) {
        return new ApiResponse(500, msg, null);
    }

    public static ApiResponse errorMap(Object data) {
        return new ApiResponse(501, "error", data);
    }

    public static ApiResponse errorTokenMsg(String msg) {
        return new ApiResponse(502, msg, null);
    }

    public static ApiResponse errorException(String msg) {
        return new ApiResponse(555, msg, null);
    }

    public static ApiResponse errorUserQQ(String msg) {
        return new ApiResponse(556, msg, null);
    }

    public ApiResponse() {

    }

    public ApiResponse(Integer code, String message, Object data) {
        this.code = code;
        this.message = message;
        this.result = data;
    }

    public ApiResponse(Integer code, String msg, Object data, String ok) {
        this.code = code;
        this.message = msg;
        this.result = data;
        this.ok = ok;
    }

    public ApiResponse(Object data) {
        this.code = 200;
        this.message = "OK";
        this.result = data;
    }

    public Boolean isOK() {
        return this.code == 200;
    }

    public static ObjectMapper getMAPPER() {
        return MAPPER;
    }

    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public Object getResult() {
        return result;
    }

    public void setResult(Object result) {
        this.result = result;
    }

    public String getOk() {
        return ok;
    }

    public void setOk(String ok) {
        this.ok = ok;
    }
}
